package com.emep.changzhi.analyse.http.basics;

import com.emep.changzhi.analyse.entity.basics.EPA;
import com.emep.changzhi.analyse.model.ResultBean;
import com.emep.changzhi.analyse.service.basics.EPAService;
import com.emep.changzhi.analyse.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@Api(value = "环保局部门模块")
@RestController
@RequestMapping(value = "/epa")
public class EPAController {

    @Autowired
    EPAService epaService;

    MapToObject<EPA> mapToObject=new MapToObject<>();

    @ApiOperation(value = "查询环保局列表", notes = "传入若干参数")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean list(@RequestBody Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "pageIndex", "pageSize", "condition");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            int pageIndex = Integer.parseInt(payload.get("pageIndex").toString());
            int pageSize = Integer.parseInt(payload.get("pageSize").toString());
            Map<String, Object> condition = (Map<String, Object>) payload.get("condition");
            Page<EPA> epaList = epaService.find(pageIndex, pageSize, condition);
            PageChunk<EPA> pageChunk = new PageChunk<>();
            if(epaList != null){
                return result.boolAdd(pageChunk.pageChunk(epaList));
            }else {
                return result.error("未查到环保局列表");
            }
        }
        return result;
    }

    @ApiOperation(value = "查询环保局详情", notes = "传入若干参数")
    @RequestMapping(value = "/info", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean info(@RequestBody Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "epaId");
        if (result.getCode() ==Constant.RESULT_CODE_SUCCESS) {
            Long epaId = Long.parseLong(payload.get("epaId").toString());
            EPA epa = epaService.getEPA(epaId);
            if (epa != null) {
                return result.boolAdd(epa);
            } else {
                return result.error("找不到环保局");
            }
        }
        return result;
    }

    @ApiOperation(value = "添加环保局信息", notes = "传入若干参数")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean add(@RequestBody Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "epa");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            Map<String, Object> map = (Map<String, Object>) payload.get("epa");
            EPA epa = new EPA();
           boolean b= mapToObject.convertEntity(epa,map);
            if (b){
                boolean flag= epaService.add(epa);
                if(flag){
                    return result.boolAdd("新增成功");
                }else {
                    return result.error("该名称已存在");
                }
            }else {
                return result.error("参数失败");
            }
        }
        return result;
    }

    @ApiOperation(value = "更新环保局信息", notes = "传入若干参数")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean update(@RequestBody Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "epa");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            Map<String, Object> map = (Map<String, Object>) payload.get("epa");
            if (map.containsKey("epaId")) {
                Long epaId = Long.parseLong(map.get("epaId").toString());
                EPA entity = epaService.getEPA(epaId);
                boolean b = mapToObject.convertEntity(entity, map);
                if (entity != null) {
                    if (b) {
                        boolean a = epaService.updateEPA(entity);
                        if (a) {
                            return result.boolAdd("修改成功");
                        } else {
                            return result.boolAdd("该名称已存在");
                        }
                    } else {
                        return result.error("参数错误");
                    }
                } else {
                    return result.boolAdd("无此环保局");
                }
            }else {
                return result.error("Id不能为空");
            }
        }
            return result;

    }

    @ApiOperation(value = "删除环保局", notes = "传入若干参数")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean delete(@RequestBody Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "epaId");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            Long epaId = Long.parseLong(payload.get("epaId").toString());
            if(epaId!=null) {
                EPA epa = epaService.getEPA(epaId);
                if (epa != null) {
                    if(!epa.getIsDelete().equals(Constant.RESULT_DELETECODE_YES)){
                        epa.setIsDelete(Constant.RESULT_DELETECODE_YES);
                        boolean a = epaService.deleteEPA(epa);
                        if (a) {
                            return result.boolAdd("删除成功");
                        } else {
                            return result.boolAdd("删除失败");
                        }
                    }else {
                        return result.error("此环保局状态为已删除,删除失败");
                    }
                } else {
                    return result.boolAdd("无此环保局");
                }
            }else {
                return result.error("EPAId为必填参数");
            }
        }
        return result;
    }


}
